﻿using System;
using ClassLib.Classes.Common;
using DevExpress.XtraSplashScreen;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Actor;
using Sumen.Business.Model.Presenter.Sys;
using Sumen_Stock.LookAndFeel.Sumen_System;

namespace Sumen_Stock.LookAndFeel.Entities
{
    public partial class Frm_Employee : DevExpress.XtraEditors.XtraForm
    {
        Frm_Employee_Edit frm_emp_edit;
        private EmployeePresenter _employeePresenter;
        private FromCode _fromCode = FromCode.FrmEmployee;

        public Frm_Employee()
        {
            InitializeComponent();
            _employeePresenter = new EmployeePresenter();
            //if (ClsPublic.Writelog == "1")
            //{
            //    SysLogPresenter log = new SysLogPresenter();
            //    log.Add(_fromCode, EnumFormStatus.View, "", "");
            //}
        }

        private void Frm_Employee_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        public override void Refresh()
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            base.Refresh();
            LoadForm(); 
            SplashScreenManager.CloseForm();
        }

        #region Button Event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            if (frm_emp_edit == null)
            {
                frm_emp_edit = new Frm_Employee_Edit();
                frm_emp_edit.EnumFormStatus = EnumFormStatus.Add;
                frm_emp_edit.ShowDialog();
            }
            else
            {
                frm_emp_edit.EnumFormStatus = EnumFormStatus.Add;
                frm_emp_edit.ShowDialog();
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonsArray1_btnEventDelete_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                if (ClsMsgBox.XacNhanXoaThongTin())
                {
                    if (_employeePresenter.Delete(sUsername)>0)
                    {
                        ClsMsgBox.ThongTin("Xóa nhân viên thành công,");
                        if (ClsPublic.Writelog == "1")
                        {
                            SysLogPresenter log = new SysLogPresenter();
                            log.Add(_fromCode, EnumFormStatus.Delete, ClsPublic.User.User_Username, "");
                        }
                    }
                }
            }
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                employee emp = _employeePresenter.GetById(sUsername);
                if (emp != null)
                {
                    if (frm_emp_edit == null)
                    {
                        frm_emp_edit = new Frm_Employee_Edit();
                        frm_emp_edit.Emp = emp;
                        frm_emp_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_emp_edit.ShowDialog();
                    }
                    else
                    {
                        frm_emp_edit.Emp = emp;
                        frm_emp_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_emp_edit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy nhân viên.");
                }
            }

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                employee emp = _employeePresenter.GetById(sUsername);
                if (emp != null)
                {
                    if (frm_emp_edit == null)
                    {
                        frm_emp_edit = new Frm_Employee_Edit();
                        frm_emp_edit.Emp = emp;
                        frm_emp_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_emp_edit.ShowDialog();
                    }
                    else
                    {
                        frm_emp_edit.Emp = emp;
                        frm_emp_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_emp_edit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy nhân viên.");
                }
            }
        }

        #endregion






        #region Method

        private void LoadForm()
        {
            gridControl1.DataSource = _employeePresenter.GetAll();
        }

        #endregion

        



    }
}